
# 怀旧
'''
R = 0.393*r + 0.769*g + 0.189*b
G = 0.349*r + 0.686*g + 0.168*b
B = 0.272*r + 0.534*g + 0.131*b
'''


# coding:utf-8
import cv2
import numpy as np
import time
import Utility.LUT as LUT


start = time.perf_counter()

def cal():
    #   怀旧
    print('计算怀旧颜色表......')
    # r
    table = np.empty([256, 256, 256])
    print('计算R通道中...')
    for r in range(0, 256):
        v1 = 0.393 * r
        for g in range(0, 256):
            v2 = v1 + 0.769 * g
            for b in range(0, 256):
                table[r, g, b] = round(v2 + 0.189 * b)
    table = np.uint8(np.clip(table, 0, 255))
    np.save('table/oldtime-r' ,table)

    # g
    print('计算G通道中...')
    table = np.empty([256, 256, 256])
    for r in range(0, 256):
        v1 = 0.349 * r
        for g in range(0, 256):
            v2 = v1 + 0.686 * g
            for b in range(0, 256):
                table[r, g, b] = round(v2 + 0.168 * b)

    table = np.uint8(np.clip(table, 0, 255))
    np.save('table/oldtime-g',table)

    # b
    print('计算B通道中...')
    table = np.empty([256, 256, 256])
    for r in range(0, 256):
        v1 = 0.272 * r
        for g in range(0, 256):
            v2 = v1 + 0.534 * g
            for b in range(0, 256):
                table[r, g, b] = round(v2 + 0.131 * b)
    table = np.uint8(np.clip(table, 0, 255))
    np.save('table/oldtime-b', table)

    end = time.perf_counter()
    print('怀旧颜色表计算完成,耗时', end-start)

cal()


# np.save('r2' ,table)
# 图像怀旧特效
# table = np.zeros([256, 256, 256], np.uint8)
img = cv2.imread('h1.jpg')
dist = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
# dist[:,:,0] = cv2.LUT(dist, table)

table = np.load('table\oldtime-r.npy')
dist[..., 0] = LUT.D3(dist, table)

table = np.load('table\oldtime-g.npy')
dist[..., 1] = LUT.D3(dist, table)

table = np.load('table\oldtime-b.npy')
dist[..., 2] = LUT.D3(dist, table)


end = time.perf_counter()
print('找查耗时', end-start)
# cv2.LUT()
# 显示图像
dist = cv2.cvtColor(dist,cv2.COLOR_RGB2BGR)
cv2.imshow('src', img)
cv2.imshow('dst', dist)
cv2.waitKey()
cv2.destroyAllWindows()
